//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: '',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  payoff: function () {
    wx.scanCode({
      success: (res) => {
        if (res.errMsg == "scanCode:ok") {
          wx.showModal({
            title: '扫码支付：',
            content: '177，799，998元将进入对方的口袋，确定支付吗？',
            success: function (res) {
              if (res.confirm) {
                //声明获取签名参数的参数
                var code = null;
                var openid = null;
                var repay_id = null;
                //登录获取code
                wx.login({
                  success: function (res) {
                    code = res.code;
                    if (code) {
                      //获取openid
                      wx.request({
                        url: 'http://www.noobkufu.cn/pay/pay/getOpenId',
                        data: {
                          code: code,
                        },
                        header: {
                          'content-type': 'application/x-www-form-urlencoded'
                        },
                        success: function (res) {
                          if (res.data.openid) {
                            //获取repay_id
                            wx.request({
                              url: 'http://www.noobkufu.cn/pay/pay/getXD',
                              data: {
                                openid: res.data.openid,
                              },
                              header: {
                                'content-type': 'application/x-www-form-urlencoded'
                              },
                              success: function (res) {
                                if (res.data.repay_id) {
                                  //获取签名信息
                                  wx.request({
                                    url: 'http://www.noobkufu.cn/pay/pay/getSign',
                                    data: {
                                      repay_id: res.data.repay_id,
                                    },
                                    header: {
                                      'content-type': 'application/x-www-form-urlencoded'
                                    },
                                    success: function (res) {
                                      if (res.data) {
                                        //微信参数
                                        var timeStamp = null;
                                        var nonceStr = null;
                                        var packAge = null;
                                        var paySign = null;
                                        //获取微信所需要的参数
                                        //例：【请求地址：http://www.noobkufu.cn/pay/pay/getSign?repay_id=11】
                                        //    【返回结果：如下】
                                        // {
                                        //  "timeStamp": "1525748213",
                                        //  "package": "prepay_id=11",
                                        //  "paySign": "350AA29BE3ABADDB467BF3BE8ABF8F64",
                                        //  "signType": "MD5",
                                        //  "nonceStr": "afpg6z9knuoyebzrsmsyrvley0lw4vej"
                                        // }
                                        wx.request({
                                          url: 'http://www.noobkufu.cn/pay/pay/getSign', //仅为示例，并非真实的接口地址
                                          data: {
                                            repay_id: repayId
                                          },
                                          method: 'post',
                                          header: {
                                            "Content-Type": "application/x-www-form-urlencoded"
                                          },
                                          success: function (res) {
                                            timeStamp = res.data.timeStamp;
                                            nonceStr = res.data.nonceStr;
                                            packAge = res.data.package;
                                            paySign = res.data.paySign;
                                          }
                                        })
                                        //判断参数是否存在，并进行相应的是否支付操作
                                        if (timeStamp == null || nonceStr == null || packAge == null || paySign == null || "" == timeStamp || "" == nonceStr || "" == packAge || "" == paySign) {
                                          wx.showToast({
                                            title: '参数错误',
                                            icon: 'loading',
                                            duration: 2000
                                          })
                                        } else {
                                          //调用支付接口
                                          wx.requestPayment({
                                            'timeStamp': timeStamp,
                                            'nonceStr': nonceStr,
                                            'package': packAge,
                                            'signType': 'MD5',
                                            'paySign': paySign,
                                            'success': function (res) {
                                              //记录交易信息
                                              wx.request({
                                                url: 'http://www.noobkufu.cn/pay/record/saveRecord',
                                                data: {
                                                  orderNumber: 'jywxzfdzxzfxt0016866' + Math.random() * 10 + '' + Math.random() * 10 + '' + Math.random() * 10
                                                },
                                                method: 'post',
                                                header: {
                                                  "Content-Type": "application/x-www-form-urlencoded"
                                                },
                                                success: function (res) {
                                                  wx.showToast({
                                                    title: 'back'
                                                  })
                                                  isPayOk = res.ok;//该接口返回的格式为{"ok": false}或{"ok": true}
                                                  if (isPayOk) {
                                                    wx.switchTab({
                                                      url: '../../pages/main/main',
                                                    }),
                                                      wx.showToast({
                                                        title: '支付记录成功',
                                                        duration: 1500
                                                      })
                                                  } else {
                                                    wx.showToast({
                                                      title: '支付记录走丢了！！',
                                                      icon: 'loading'
                                                    })
                                                  }
                                                },
                                                fail: function (res) {
                                                  wx.showToast({
                                                    title: '网络走丢了！！',
                                                    icon: 'loading'
                                                  })
                                                }
                                              })
                                            },
                                            'fail': function (res) {
                                              wx.showToast({
                                                title: '支付失败！！',
                                                icon: 'loading'
                                              })
                                            }
                                          })
                                        }
                                      } else {
                                        wx.showToast({
                                          title: '获取签名失败！',
                                          icon: 'loading',
                                          duration: 2000
                                        })
                                      }
                                    }
                                  })
                                } else {
                                  wx.showToast({
                                    title: '获取repay_id失败！',
                                    icon: 'loading',
                                    duration: 2000
                                  })
                                }
                              }
                            })
                          } else {
                            wx.showToast({
                              title: '获取openid失败！',
                              icon: 'loading',
                              duration: 2000
                            })
                          }
                        }
                      })
                    } else {
                      wx.showToast({
                        title: '获取code失败！',
                        icon: 'loading',
                        duration: 2000
                      })
                    }
                  }
                });
              } else if (res.cancel) {
                wx.switchTab({
                  url: '../../pages/index/index',
                }),
                  wx.showToast({
                    title: '成功取消！',
                    duration: 1500
                  })
              }
            }
          })
        } else {
          wx.showModal({
            title: '支付失败：',
            content: '网络跑丢了，稍后再试~~',
            success: function (res) {
              if (res.confirm) {
                wx.switchTab({
                  url: '../../pages/index/index',
                })
              } else if (res.cancel) {
                wx.switchTab({
                  url: '../../pages/index/index',
                })
              }
            }
          })
        }
      }
    });
  }
})
